﻿using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Core;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ReportModel
{
   public class Report
    {
        public string thisWeek;
        public string nextWeek;
        public DateTime nextWeekStart;
        public DateTime nextWeekEnd;
        public int thisWeekCellRow;
        public int thisWeekCellColumn;
        public int nextWeekCellRow;
        public int nextWeekCellColumn;
        public   bool editDoc(string filePath)
        {
            if (File.Exists(filePath))
            {

                try
                {
                    object oMissing = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word._Application wordApplication;
                    Microsoft.Office.Interop.Word._Document document;
                    wordApplication = new Microsoft.Office.Interop.Word.Application();
                    wordApplication.Visible = false; object fileName = @filePath;
                    document = wordApplication.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    
                    Table table = document.Tables[1] as Table;
                    Regex reg1 = new Regex(@"(?<=\()[^\)]+", RegexOptions.IgnoreCase);
                    Regex reg2 = new Regex(@"(?<=\（)[^\）]+", RegexOptions.IgnoreCase);
                    string thisWeekStr = table.Cell(thisWeekCellRow, thisWeekCellColumn).Range.Text.Trim();

                    thisWeekStr = reg1.Replace(thisWeekStr, this.thisWeek, 1);
                    thisWeekStr = reg2.Replace(thisWeekStr, this.thisWeek, 1);
                    table.Cell(thisWeekCellRow, thisWeekCellColumn).Range.Text = thisWeekStr;
                    string nextWeekStr = reg1.Replace(table.Cell(nextWeekCellRow, nextWeekCellColumn).Range.Text, this.thisWeek);
                    nextWeekStr = reg1.Replace(nextWeekStr, this.nextWeek, 1);
                    nextWeekStr = reg2.Replace(nextWeekStr, this.nextWeek, 1);
                    table.Cell(nextWeekCellRow, nextWeekCellColumn).Range.Text = nextWeekStr;

                  
                    document.Save();
                    document.Close();
                    wordApplication.Quit();
                    return true;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return false;

                }
                finally
                {
                }
            }
                    return false;
        }



    }
}
